package com.ruoyi.jihuo.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jihuo.domain.JhSysTrafficRecord;
import com.ruoyi.jihuo.domain.search.JhSysTrafficRecordSearch;
import com.ruoyi.jihuo.service.IJhSysTrafficRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/jihuo/sysTrafficRecord")
public class JhSysTrafficRecordController extends BaseController {


    @Autowired
    private IJhSysTrafficRecordService IJhSysTrafficRecordService;

    /**
     * 查询流量加密串记录列表
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:list')")
    @GetMapping("/list")
    public TableDataInfo sysTrafficRecordList(JhSysTrafficRecordSearch sysTrafficRecord) {
        startPage();
        List<JhSysTrafficRecord> list = IJhSysTrafficRecordService.selectJhSysTrafficRecordList(sysTrafficRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:list')")
    @GetMapping("/getCurrentUserTrafficRecord")
    public TableDataInfo getCurrentUserTrafficRecord() {
        startPage();
        List<JhSysTrafficRecord> list = IJhSysTrafficRecordService.getCurrentUserTrafficRecord();
        return getDataTable(list);
    }

    /**
     * 插入流量加密串记录
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:insert')")
    @Log(title = "加密串管理", businessType = BusinessType.INSERT)
    @PostMapping("/insertSysTrafficRecord")
    public AjaxResult insertSysTrafficRecord(@Validated @RequestBody JhSysTrafficRecord sysTrafficRecord) {
        return toAjax(IJhSysTrafficRecordService.insertJhSysTrafficRecord(sysTrafficRecord));
    }

    /**
     * 修改加密串
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:edit')")
    @Log(title = "加密串管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateJhSysTrafficRecord")
    public AjaxResult edit(@Validated @RequestBody JhSysTrafficRecord sysTrafficRecord) {
        return toAjax(IJhSysTrafficRecordService.updateJhSysTrafficRecord(sysTrafficRecord));
    }

    /**
     * 充值 ，目前仅支持一卡一充
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:edit')")
    @Log(title = "充值", businessType = BusinessType.UPDATE)
    @PutMapping("/rechargeTraffic")
    public AjaxResult rechargeTraffic(@Validated @RequestBody JhSysTrafficRecord sysTrafficRecord) {
        String res = IJhSysTrafficRecordService.rechargeTraffic(sysTrafficRecord);

        if("1".equals(res)){
            return success();
        } else {
            return AjaxResult.error(res);
        }

    }

    /**
     * 禁用加密串
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:enabled')")
    @Log(title = "加密串管理", businessType = BusinessType.OTHER)
    @PutMapping(value = {"/disableRecord/{id}"})
    public AjaxResult disable(@PathVariable(value = "id", required = false) Long id) {
        return toAjax(IJhSysTrafficRecordService.disableJhSysTrafficRecord(id));
    }

    /**
     * 启用加密串
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:enabled')")
    @Log(title = "加密串管理", businessType = BusinessType.OTHER)
    @PutMapping(value = {"/enableRecord/{id}"})
    public AjaxResult enable(@PathVariable(value = "id", required = false) Long id) {
        return toAjax(IJhSysTrafficRecordService.enableJhSysTrafficRecord(id));
    }

    /**
     * 删除加密串
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:delete')")
    @Log(title = "加密串管理", businessType = BusinessType.OTHER)
    @DeleteMapping(value = {"/deleteRecord/{id}"})
    public AjaxResult delete(@PathVariable(value = "id", required = false) Long id) {
        return toAjax(IJhSysTrafficRecordService.deleteJhSysTrafficRecordById(id));
    }

    /**
     * 根据id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('jh:SysTrafficRecord:query')")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id)) {
            JhSysTrafficRecord sysTrafficRecord = IJhSysTrafficRecordService.selectJhSysTrafficRecordById(id);
            ajax.put(AjaxResult.DATA_TAG, sysTrafficRecord);
        }
        return ajax;
    }

}
